{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn.functional as F\n",
    "from torch.autograd import Variable\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "argument 0 is not a Variable",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-2-69dadb3ab1fb>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mTensor\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m8\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mF\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mconv2d\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrandn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m/Users/vincentherrmann/anaconda3/lib/python3.5/site-packages/torch/nn/functional.py\u001b[0m in \u001b[0;36mconv2d\u001b[0;34m(input, weight, bias, stride, padding, dilation, groups)\u001b[0m\n\u001b[1;32m     37\u001b[0m     f = ConvNd(_pair(stride), _pair(padding), _pair(dilation), False,\n\u001b[1;32m     38\u001b[0m                _pair(0), groups, torch.backends.cudnn.benchmark, torch.backends.cudnn.enabled)\n\u001b[0;32m---> 39\u001b[0;31m     \u001b[0;32mreturn\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mweight\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbias\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     40\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     41\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mTypeError\u001b[0m: argument 0 is not a Variable"
     ],
     "output_type": "error"
    }
   ],
   "source": [
    "x = torch.Tensor(2, 8)\n",
    "F.conv2d(x, torch.randn(2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "\n",
       "Columns 0 to 5 \n",
       " 0.0000e+00  0.0000e+00  4.6330e-29  1.5846e+29  1.3189e-30  1.4013e-45\n",
       "\n",
       "Columns 6 to 7 \n",
       " 9.1457e-31  1.8367e-40\n",
       "[torch.FloatTensor of size 1x8]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.Tensor(1, 8)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x:  \n",
      " 0.0444\n",
      " 0.0401\n",
      " 0.3915\n",
      " 0.1426\n",
      " 0.9050\n",
      " 0.1053\n",
      " 0.9096\n",
      " 0.3363\n",
      "[torch.FloatTensor of size 8x1]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "x = torch.rand(8, 1)\n",
    "print('x: ', x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x as 8x1:  \n",
      " 0.0444\n",
      " 0.0401\n",
      " 0.3915\n",
      " 0.1426\n",
      " 0.9050\n",
      " 0.1053\n",
      " 0.9096\n",
      " 0.3363\n",
      "[torch.FloatTensor of size 8x1]\n",
      "\n",
      "x as 4x2:  \n",
      " 0.0444  0.0401\n",
      " 0.3915  0.1426\n",
      " 0.9050  0.1053\n",
      " 0.9096  0.3363\n",
      "[torch.FloatTensor of size 4x2]\n",
      "\n",
      "x as 2x4:  \n",
      " 0.0444  0.0401  0.3915  0.1426\n",
      " 0.9050  0.1053  0.9096  0.3363\n",
      "[torch.FloatTensor of size 2x4]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print('x as 8x1: ', x.view(8,1))\n",
    "print('x as 4x2: ', x.view(4,2))\n",
    "print('x as 2x4: ', x.view(2,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "[m, n] = x.size()\n",
    "print(m)\n",
    "print(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " 0.7126  0.4438\n",
      " 0.9070  0.5184\n",
      " 0.2700  0.6687\n",
      " 0.8817  0.6159\n",
      " 0.0000  0.0000\n",
      " 0.0000  0.0000\n",
      " 0.0000  0.0000\n",
      " 0.0000  0.0000\n",
      " 0.0000  0.0000\n",
      "[torch.FloatTensor of size 9x2]\n",
      "\n",
      "Variable containing:\n",
      " 0.0000e+00  0.0000e+00\n",
      " 8.2210e-29 -8.5899e+09\n",
      "[torch.FloatTensor of size 2x2]\n",
      "\n",
      "Variable containing:\n",
      " 7.1258e-01  4.4381e-01\n",
      " 9.0704e-01  5.1841e-01\n",
      " 2.7005e-01  6.6866e-01\n",
      " 8.8166e-01  6.1589e-01\n",
      " 1.3659e-30  1.4013e-45\n",
      " 1.3663e-30  1.4013e-45\n",
      " 0.0000e+00  0.0000e+00\n",
      " 0.0000e+00  0.0000e+00\n",
      " 1.4911e-37  1.4013e-45\n",
      " 0.0000e+00  0.0000e+00\n",
      " 8.2210e-29 -8.5899e+09\n",
      "[torch.FloatTensor of size 11x2]\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\n",
       " 7.1258e-01  4.4381e-01\n",
       " 9.0704e-01  5.1841e-01\n",
       " 2.7005e-01  6.6866e-01\n",
       " 8.8166e-01  6.1589e-01\n",
       " 1.3659e-30  1.4013e-45\n",
       " 1.3663e-30  1.4013e-45\n",
       " 0.0000e+00  0.0000e+00\n",
       " 0.0000e+00  0.0000e+00\n",
       " 1.4911e-37  1.4013e-45\n",
       " 0.0000e+00  0.0000e+00\n",
       " 8.2210e-29 -8.5899e+09\n",
       "[torch.FloatTensor of size 11x2]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.rand(8, 1)\n",
    "x = x.resize_(9, 2)\n",
    "print(x)\n",
    "x = Variable(x)\n",
    "\n",
    "y = Variable(torch.FloatTensor(2, 2))\n",
    "print(y)\n",
    "\n",
    "z = torch.cat((x, y), 0)\n",
    "print(z)\n",
    "\n",
    "z.data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 2\n",
    "a *= 3\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1 // 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 x 64:  \n",
      "\n",
      "Columns 0 to 12 \n",
      "    0     1     2     3     4     5     6     7     8     9    10    11    12\n",
      "\n",
      "Columns 13 to 25 \n",
      "   13    14    15    16    17    18    19    20    21    22    23    24    25\n",
      "\n",
      "Columns 26 to 38 \n",
      "   26    27    28    29    30    31    32    33    34    35    36    37    38\n",
      "\n",
      "Columns 39 to 51 \n",
      "   39    40    41    42    43    44    45    46    47    48    49    50    51\n",
      "\n",
      "Columns 52 to 63 \n",
      "   52    53    54    55    56    57    58    59    60    61    62    63\n",
      "[torch.FloatTensor of size 1x64]\n",
      "\n",
      "2 x 32:  \n",
      "\n",
      "Columns 0 to 12 \n",
      "    0     2     4     6     8    10    12    14    16    18    20    22    24\n",
      "    1     3     5     7     9    11    13    15    17    19    21    23    25\n",
      "\n",
      "Columns 13 to 25 \n",
      "   26    28    30    32    34    36    38    40    42    44    46    48    50\n",
      "   27    29    31    33    35    37    39    41    43    45    47    49    51\n",
      "\n",
      "Columns 26 to 31 \n",
      "   52    54    56    58    60    62\n",
      "   53    55    57    59    61    63\n",
      "[torch.FloatTensor of size 2x32]\n",
      "\n",
      "4 x 16:  \n",
      "\n",
      "Columns 0 to 12 \n",
      "    0     4     8    12    16    20    24    28    32    36    40    44    48\n",
      "    1     5     9    13    17    21    25    29    33    37    41    45    49\n",
      "    2     6    10    14    18    22    26    30    34    38    42    46    50\n",
      "    3     7    11    15    19    23    27    31    35    39    43    47    51\n",
      "\n",
      "Columns 13 to 15 \n",
      "   52    56    60\n",
      "   53    57    61\n",
      "   54    58    62\n",
      "   55    59    63\n",
      "[torch.FloatTensor of size 4x16]\n",
      "\n",
      "8 x 8:  \n",
      "    0     8    16    24    32    40    48    56\n",
      "    1     9    17    25    33    41    49    57\n",
      "    2    10    18    26    34    42    50    58\n",
      "    3    11    19    27    35    43    51    59\n",
      "    4    12    20    28    36    44    52    60\n",
      "    5    13    21    29    37    45    53    61\n",
      "    6    14    22    30    38    46    54    62\n",
      "    7    15    23    31    39    47    55    63\n",
      "[torch.FloatTensor of size 8x8]\n",
      "\n",
      "1 x 64:  \n",
      "\n",
      "Columns 0 to 12 \n",
      "    0     1     2     3     4     5     6     7     8     9    10    11    12\n",
      "\n",
      "Columns 13 to 25 \n",
      "   13    14    15    16    17    18    19    20    21    22    23    24    25\n",
      "\n",
      "Columns 26 to 38 \n",
      "   26    27    28    29    30    31    32    33    34    35    36    37    38\n",
      "\n",
      "Columns 39 to 51 \n",
      "   39    40    41    42    43    44    45    46    47    48    49    50    51\n",
      "\n",
      "Columns 52 to 63 \n",
      "   52    53    54    55    56    57    58    59    60    61    62    63\n",
      "[torch.FloatTensor of size 1x64]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "t = torch.linspace(0, 63, steps=64)\n",
    "t = t.view(1, 64)\n",
    "print('1 x 64: ', t)\n",
    "\n",
    "t = t.transpose(0, 1).contiguous()\n",
    "t = t.view(32, 2)\n",
    "t = t.transpose(0, 1)\n",
    "print('2 x 32: ', t)\n",
    "\n",
    "t = t.transpose(0, 1).contiguous()\n",
    "t = t.view(16, 4)\n",
    "t = t.transpose(0, 1)\n",
    "print('4 x 16: ', t)\n",
    "\n",
    "t = t.transpose(0, 1).contiguous()\n",
    "t = t.view(8, 8)\n",
    "t = t.transpose(0, 1).contiguous()\n",
    "print('8 x 8: ', t)\n",
    "\n",
    "t = t.transpose(0, 1).contiguous()\n",
    "t = t.view(1, 64)\n",
    "\n",
    "print('1 x 64: ', t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "Columns 0 to 12 \n",
      "   10    11    12    13    14    15    16    17    18    19    20    21    22\n",
      "\n",
      "Columns 13 to 25 \n",
      "   23    24    25    26    27    28    29    30    31    32    33    34    35\n",
      "\n",
      "Columns 26 to 38 \n",
      "   36    37    38    39    40    41    42    43    44    45    46    47    48\n",
      "\n",
      "Columns 39 to 51 \n",
      "   49    50    51    52    53    54    55    56    57    58    59    60    61\n",
      "\n",
      "Columns 52 to 53 \n",
      "   62    63\n",
      "[torch.FloatTensor of size 1x54]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "t = t[:, 5:]\n",
    "print(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " 15\n",
      "[torch.FloatTensor of size 1]\n",
      "\n",
      "\n",
      "  2\n",
      "  3\n",
      "  4\n",
      "  5\n",
      "  6\n",
      "  7\n",
      "  8\n",
      "  9\n",
      " 10\n",
      " 11\n",
      " 12\n",
      " 13\n",
      " 14\n",
      "[torch.FloatTensor of size 13]\n",
      "\n",
      "\n",
      " 14\n",
      " 15\n",
      "[torch.FloatTensor of size 2]\n",
      "\n",
      "\n",
      " 7.0000\n",
      " 7.5000\n",
      "[torch.FloatTensor of size 2]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "t = torch.linspace(0, 15, steps=16)\n",
    "s = t[-1:]\n",
    "print(s)\n",
    "s = t[-14:-1:1]\n",
    "print(s)\n",
    "s = t[-2:]\n",
    "print(s)\n",
    "s = s / 2\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7.5\n"
     ]
    }
   ],
   "source": [
    "print(torch.max(s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    ""
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3.0
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}